<?php
	
function keperluan_label_list($biaya_id ){
	$query = "SELECT * FROM spd_keperluan a
	INNER JOIN spd_keperluan_biaya b ON a.keperluan_id = b.keperluan_id
	WHERE b.biaya_id ={$biaya_id}";
	$result = my_query($query);
	$r ='';
	while($row = my_fetch_array($result)){
		$r .= $row['label_keperluan'].', ';
	} 
	
	return rtrim( trim($r),',' );
}

function kendaraan_label_list($biaya_id ){
	$query = "SELECT * FROM spd_kendaraan a
	INNER JOIN spd_kendaraan_biaya b ON a.kendaraan_id = b.kendaraan_id
	WHERE b.biaya_id ={$biaya_id}";
	$result = my_query($query);
	$r ='';
	while($row = my_fetch_array($result)){
		$r .= $row['label_kendaraan'].', ';
	} 
	
	return rtrim( trim($r),',' );
}

function list_biaya(){

//PASANG JAVASCRIPT
my_set_code_js('

function confirmDelete(id){
	var t = confirm(\'Yakin akan menghapus data ?\');
	if(t){
		location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
	}
	return false;
}

');
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'kode'=>array('style'=>'border-bottom:2px solid;width:10%'), 
		'nama biaya'=>array('style'=>'border-bottom:2px solid;20%'), 
		'keperluan'=>array('style'=>'border-bottom:2px solid;35%'), 
		'kendaraan'=>array('style'=>'border-bottom:2px solid;20%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM spd_biaya ORDER BY kode_biaya";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=edit&id='.$ey['biaya_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

	
		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['biaya_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'kode'=>  $ey['kode_biaya'] ,   
			'divisi'=>  $ey['label_biaya'] ,   
			'keperluan'=>  keperluan_label_list($ey['biaya_id']) ,   
			'kendaraan'=>  kendaraan_label_list($ey['biaya_id']) ,   
			'operasi'=> position_text_align(  $edit_button .' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);
	return table_builder($header , $datas ,  3 , false );
}

function form_biaya_create($id = 0){
	$view = form_header( "devisi" , "devisi"  );
 	$fields = my_get_data_by_id('spd_biaya','biaya_id',$id);
	$temp = array(
				'name'=>'kode_biaya',
				'value'=>( isset($_POST['kode_biaya']) ? $_POST['kode_biaya'] : $fields['kode_biaya'] ),
				'id'=>'kode_biaya',
				'type'=>'text',
				'size'=>'10'
			);
	$form_temp = form_dynamic($temp);	
	$view .= form_field_display( $form_temp   , "Kode biaya"   );
	$temp = array(
				'name'=>'label_biaya',
				'value'=>( isset($_POST['label_biaya']) ? $_POST['label_biaya'] : $fields['label_biaya'] ),
				'id'=>'label_biaya',
				'type'=>'text',
				'size'=>'35'
			);
	$form_temp = form_dynamic($temp);	
	$view .= form_field_display( $form_temp   , "Nama biaya"   );
	
	$query = "SELECT * FROM spd_keperluan ";
	$result = my_query($query);
	$t='';
	while($keperluan = my_fetch_array($result)){
		$t .=list_each_keperluan($keperluan ,($fields? $fields['biaya_id']:0));
	}
	$view .= form_field_display( $t   , "Keperluan"   );
	
	$query = "SELECT * FROM spd_kendaraan ";
	$result = my_query($query);
	$t='';
	while($temps = my_fetch_array($result)){
		$t .=list_each_kendaraan($temps ,($fields? $fields['biaya_id']:0));
	}
	$view .= form_field_display( $t   , "Kendaraan"   );

	if($fields['tabel_transport']=='Y'){ 
		$temp = array(
			'name'=>'tabel_transport',
			'id'=>'tabel_transport',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$temp = array(
			'name'=>'tabel_transport',
			'id'=>'tabel_transport',
			'type'=>'checkbox',
		); 
	}
	$form_temp = form_dynamic($temp).'ditampilkan' ;
	$view .= form_field_display( $form_temp , "Tabel Transport" );

	$temp = array(
				'name'=>'penyesuaian_hari',
				'value'=>( isset($_POST['penyesuaian_hari']) ? $_POST['penyesuaian_hari'] : $fields['penyesuaian_hari'] ),
				'id'=>'penyesuaian_hari',
				'type'=>'text',
				'size'=>'3'
			);
	$form_temp = form_dynamic($temp);	
	$view .= form_field_display( $form_temp   , "Penyesuaian Hari"   );

	$atemps = array(
		'Nominal'=>'Nominal',
		'Maksimal'=>'Maksimal',
	);
	$temp = array(
		'name'=>'tipe',
		'value'=>( isset($_POST['tipe']) ? $_POST['tipe'] : $fields['tipe'] ),
		'id'=>'tipe' 
	);
	$form_temp = form_radiobutton($temp ,$atemps);
	$view .= form_field_display( $form_temp   , "Tipe"   );

	$atemps = array();
	$query = "SELECT * FROM spd_biaya WHERE biaya_id<>{$id} ORDER BY label_biaya";
	$result = my_query($query);
	while($row = my_fetch_array($result) ){
		$atemps[$row['biaya_id']] =$row['label_biaya'];
	}
	$temp = array(
		'name'=>'link_biaya_id',
		'id'=>'link_biaya_id',
		'value'=>isset($_POST['link_biaya_id']) ? $_POST['link_biaya_id'] : $fields['link_biaya_id'],
	);
	$form_temp = form_dropdown($temp , $atemps);
	$view .= form_field_display( $form_temp , "Di total dengan biaya" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
		$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}


function form_biaya_validate($id){
	$errsubmit = false;
	$err = array();
	 
	$code =  trim($_POST["kode_biaya"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Kode biaya belum di isi";
	}
	$query="SELECT biaya_id FROM spd_biaya 
		WHERE kode_biaya='".$code.
		"' AND biaya_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Kode biaya sudah ada";	
	}	

	$code =  trim($_POST["label_biaya"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Nama biaya belum diisi";
	}
	
	if(isset($_POST['k'])){
	}else{
		$errsubmit = true;
		$err[] = "Keperluan tidak ada yang dipilih";
	}

	if(isset($_POST['kd'])){
	}else{
		$errsubmit = true;
		$err[] = "Kendaraan tidak ada yang dipilih";
	}

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function form_biaya_submit($id){
	$query = "DELETE FROM spd_keperluan_biaya WHERE biaya_id ={$id}";
	my_query($query);
	$query = "DELETE FROM spd_kendaraan_biaya WHERE biaya_id ={$id}";
	my_query($query);

	$datas = array(
		'kode_biaya'=>my_type_data_str( $_POST['kode_biaya'] ),
		'label_biaya'=>my_type_data_str( $_POST['label_biaya'] ), 
		'penyesuaian_hari'=>my_type_data_int( $_POST['penyesuaian_hari'] ),		
		'tabel_transport'=>my_type_data_str( isset($_POST['tabel_transport']) ? 'Y': 'T' ), 
		'tipe'=>my_type_data_str( $_POST['tipe'] ), 
		'link_biaya_id'=>my_type_data_int( $_POST['link_biaya_id'] ), 
	);
	
	if($id > 0){
		submit_each_keperluan($id);
		submit_each_kendaraan($id);
		return my_update_record('spd_biaya' ,	'biaya_id' , $id , $datas);
	}
	$id = my_insert_record('spd_biaya' , $datas);
	submit_each_keperluan($id);
	submit_each_kendaraan($id);
	return  true;
}

function remove_biaya($id){
	$query= "DELETE FROM spd_biaya WHERE biaya_id = {$id}";
	return my_query($query);
} 
 
function submit_each_keperluan($id){
 
	foreach($_POST['k'] as $keperluan_id=>$rv){ 
		if(isset($_POST['k'][$keperluan_id])){
			$datas = array(
				'biaya_id'	=> my_type_data_str($id),
				'keperluan_id'	=> my_type_data_str($keperluan_id),
			);	
		$t =	my_insert_record('spd_keperluan_biaya' , $datas);
		 
		}
	}
}

function submit_each_kendaraan($id){
 
	foreach($_POST['kd'] as $kendaraan_id=>$rv){ 
		if(isset($_POST['kd'][$kendaraan_id])){
			$datas = array(
				'biaya_id'	=> my_type_data_str($id),
				'kendaraan_id'	=> my_type_data_str($kendaraan_id),
			);	
		$t =	my_insert_record('spd_kendaraan_biaya' , $datas);
		 
		}
	}
}
 
function list_each_keperluan($keperluan , $biaya_id){
	$check ="";
	$query = "SELECT * FROM spd_keperluan_biaya WHERE keperluan_id = {$keperluan['keperluan_id']} AND biaya_id ={$biaya_id}";
	$result = my_query($query);
	if(my_num_rows($result)){
		$check = ' checked="checked" ';
	}
	return'<div style="float:left;width:200px;"><input '.$check.' type="checkbox" value="a" name="k['.$keperluan['keperluan_id'].']"/> '.$keperluan['label_keperluan'].'</div>';
} 
function list_each_kendaraan($kendaraan , $biaya_id){
	$check ="";
	$query = "SELECT * FROM spd_kendaraan_biaya WHERE kendaraan_id = {$kendaraan['kendaraan_id']} AND biaya_id ={$biaya_id}";
	$result = my_query($query);
	if(my_num_rows($result)){
		$check = ' checked="checked" ';
	}
	return'<div style="float:left;width:200px;"><input '.$check.' type="checkbox" value="a" name="kd['.$kendaraan['kendaraan_id'].']"/> '.$kendaraan['label_kendaraan'].'</div>';
} 